<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Article;
use app\common\model\ArticleCate;
use app\common\model\Content;
use app\common\model\VideoCate;
use app\common\model\VideoType;
use app\common\validate\Video;
use think\facade\Request;
use think\facade\View;
use think\exception\ValidateException;

class AdminYouku extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
        $type = VideoType::select();
        View::assign("type",$type);
        $cate = VideoCate::select();
        View::assign("cate",$cate);

    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $keyword = request()->param("keyword");
        $search_field = request()->param("search_field");
        if($keyword && $search_field){
            if ($search_field == 'id'){
                $where[] = ["{$search_field}",'=',"{$keyword}"];
            }else{
                $where[] = ["{$search_field}",'like',"%{$keyword}%"];
            }
        }
        if(!isset($where)){
            $where = 1;
        }
        $list = \app\common\model\Video::with(["cate"])
            ->where($where)
            ->order("id DESC")
            ->paginate(["query"=>request()->param()]);
        //echo Article::getlastsql();die;
        View::assign("list",$list);
        View::assign("title","文章列表");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {


        View::assign("res",[]);
        View::assign("title","文章发布");
        return \view();
    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {


        $user = $this->user;
        $d = input("post.");
        $d["userinfo"] = json_encode($user);


        try {
            validate(Video::class)->check($d);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return  json(["code"=>4000,"msg"=>$e->getError()]);
        }

        $c_mod = new Content();
        $d["uid"] = $user["id"];
        $d["model_id"] = 2;
        $content = $d;
        $content["info"] = $d["desc"];//文章的主内容
        //p($content);die;
        if(isset($content["id"])){
            //修改
            Content::update($content);
            $contentid = $content["id"];
        }else{
            //新增
            $content_res = $c_mod->save($content);
            $contentid = $c_mod->id;
        }




        $d["contentid"] = $contentid;
        if(isset($content["id"])){
            $d["id"] = $d["videoid"];
            $res = \app\common\model\Video::update($d);
        }else{
            $res = \app\common\model\Video::create($d);
        }


        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

    //添加短视频
    public function minvideo()
    {
        if(\request()->isAjax()){
            $d = \request()->post();
            $d["intro"] = $d["info"];
            $d["desc"] = $d["info"];
            $d["last_time"] = time();
            $contentid = $d["id"];
            unset($d["add_time"]);
            unset($d["delete_time"]);
            unset($d["id"]);

            $res = Content::update($d,["id"=>$contentid]);
            $res1 = \app\common\model\Video::update($d,["contentid"=>$contentid]);

            if($res && $res1){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }
        View::assign("res",[]);
        View::assign("title","文章发布");
        return \view();
    }

  

    

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = \app\common\model\Video::with(["con"])->find($id)->toArray();
        View::assign("res",$res);

        return view('create');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = ArticleCate::where("id",$id)->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

}
